﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccess.Repository.Interface;
using DataAccess.Repository;

namespace KPI
{
    public partial class PolicyDocumentsList :BasePage
    {
        private IPolicyDocuments ipd = new IMPPolicyDocument();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                var u = KPISiteContext.Current.CurrentUser;
                if (u != null && u.RoleName == "administrators") {
                  this.btnDeleteSelected.Visible = true;
                }

                BindDatePolicyDocuments(null, 1, 0, 0);
            }
        }

        private void BindDatePolicyDocuments(string keys, int pageindex, int PageCount, int rowCount) 
        {
            RPPolicyDocumentsList.DataSource = ipd.GetPolicyDocumentsListPageing(keys, 20, pageindex, out PageCount, out rowCount);
            RPPolicyDocumentsList.DataBind();

            PageingPar(pageindex, PageCount, rowCount);
        }

        private void PageingPar(int pageindex, int PageCount, int rowCount)
        {
            PageControlPD.PageIndex = pageindex;
            PageControlPD.PageCount = PageCount;
            PageControlPD.rowcount = rowCount;
            Literal lrowcount = PageControlPD.FindControl("lTotalRecords") as Literal;
            lrowcount.Text = "总记录: "+rowCount;
            Literal lPageIndex = PageControlPD.FindControl("lPageIndex") as Literal;
            lPageIndex.Text = pageindex.ToString();
            Literal lPageCount = PageControlPD.FindControl("lPageCount") as Literal;
            lPageCount.Text = PageCount.ToString();
        }
        protected void PageControlPD_Indexchange(object sender, EventArgs e) 
        {
            BindDatePolicyDocuments(string.IsNullOrWhiteSpace(txttitle.Text.Trim()) ? null : txttitle.Text.Trim(), PageControlPD.PageIndex, 0, 0);
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindDatePolicyDocuments(string.IsNullOrWhiteSpace(txttitle.Text.Trim()) ? null : txttitle.Text.Trim(), 1, 0, 0);
        }

        protected void btnDeleteSelected_Click(object sender, EventArgs e)
        {
            List<int> pdIDs = new List<int>();
            int dpitems = RPPolicyDocumentsList.Items.Count;

            if (dpitems > 0)
            {
                for (int i = 0; i <  RPPolicyDocumentsList.Items.Count; i++)
                {
                    CheckBox chbSelect = RPPolicyDocumentsList.Items[i].FindControl("chbSelect") as CheckBox;
                     if (chbSelect.Checked)
                     {
                         var id = Convert.ToInt32(chbSelect.Attributes["rel"]);

                         pdIDs.Add(id);
                     }
                }
               
            }

            if (pdIDs.Count > 0)
            {

                if (ipd.DeletePolicyDocuments(pdIDs))
                {
                    BindDatePolicyDocuments(null, 1, 0, 0);
                    ShowMessage("删除成功.", false);
                }
                else
                {
                    ShowMessage("删除时遇到错误,请稍后再试或联系管理员.", true);
                }
            }
        }
    }
}